package br.edu.ufcg.embedded.eolica.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import br.edu.ufcg.embedded.eolica.MIDlet.UsinaEolicaMIDlet;
import br.edu.ufcg.embedded.eolica.domain.User;
import br.edu.ufcg.embedded.eolica.exception.FacadeException;
import br.edu.ufcg.embedded.eolica.facade.Facade;
import br.edu.ufcg.embedded.eolica.util.MyCallback;
import br.edu.ufcg.embedded.eolica.util.Properties;

public class PersonalInfoForm extends Form implements CommandListener {

	private Display d;
	private Displayable previous;
	private Command cmdSave, cmdBack, cmdDelete;
	private TextField usernameField, passwordField;
	private User user;
	private Properties prop;
	private Facade facade;

	public PersonalInfoForm(Display d, String personal, Displayable previous) {
		super("Eletrobras - " + personal);
		this.d = d;
		this.previous = previous;
		facade = Facade.getInstance();
		loadComponents();
		loadData();
	}

	private void loadComponents() {
		prop = UsinaEolicaMIDlet.getProp();

		usernameField = new TextField(prop.getProperty("label.username"), "",
				32, TextField.ANY);
		passwordField = new TextField(prop.getProperty("label.password"), "",
				32, TextField.ANY);

		cmdBack = new Command(prop.getProperty("label.back"), Command.BACK, 0);
		cmdSave = new Command(prop.getProperty("label.save"), Command.OK, 0);
		cmdDelete = new Command(prop.getProperty("label.delete"),
				Command.CANCEL, 0);

		append(usernameField);
		append(passwordField);
		addCommand(cmdBack);
		addCommand(cmdSave);
		addCommand(cmdDelete);
		setCommandListener(this);
	}

	private void loadData() {
		try {
			user = facade.getUser();
			if (user != null) {
				usernameField.setString(user.getUsername());
				passwordField.setConstraints(TextField.PASSWORD);
				passwordField.setString(user.getPassword());
			}
		} catch (FacadeException e) {
			d.setCurrent(new ErrorAlert(prop.getProperty("label.error"), prop
					.getProperty("error.userload")), this);
		}
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == cmdBack) {
			d.setCurrent(previous);
		} else if (cmd == cmdSave) {
			if (usernameField.getString().length() == 0
					|| passwordField.getString().length() == 0)
				d.setCurrent(new ErrorAlert(prop.getProperty("label.error"),
						prop.getProperty("error.user")), this);
			else {
				try {
					if (user == null) {
						user = new User(usernameField.getString(), passwordField.getString());
						facade.saveUser(user);
					} else {
						user.setUsername(usernameField.getString());
						user.setPassword(passwordField.getString());
						facade.saveUser(user);
					}
					d.setCurrent(new SuccessAlert(prop.getProperty("label.success"), prop.getProperty("label.successdata")), this);
				} catch (FacadeException e) {
					d.setCurrent(new ErrorAlert(prop.getProperty("label.error"), prop.getProperty("error.usersave")), this);
				}
			}
		} else if (cmd == cmdDelete) {
			QuestionAlert question = new QuestionAlert(prop.getProperty("label.question"), prop.getProperty("label.questionmessage"), this);
			d.setCurrent(question);
		} else if (cmd.getLabel().equals(QuestionAlert.YES)) {
			try {
				facade.deleteUser();
				user = null;
				usernameField.setString("");
				passwordField.setString("");
				passwordField.setConstraints(TextField.ANY);
				d.setCurrent(new SuccessAlert(prop.getProperty("label.success"), prop.getProperty("label.successremove")), this);
			} catch (FacadeException e) {
				d.setCurrent(new ErrorAlert(prop.getProperty("label.error"),
						prop.getProperty("error.userdelete")), this);
			}
			d.setCurrent(this);
		} else if (cmd.getLabel().equals(QuestionAlert.NO)) {
			d.setCurrent(this);
		}
	}

}
